<?php

namespace App\Http\Controllers\Administrator\Game;

use App\Models\Log\PlayerTransfer;
use App\Models\Log\SiteScore;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PlayerTransferController extends Controller
{
    use Helpers;
    //查询
    public function index(Request $request,PlayerTransfer $playerTransfer)
    {
        $this->authorize('index', $playerTransfer);
        $limit = $request->limit ?? config('administrator.paginate.limit');
        $params = [
            'status' => '',
            'transferid' => ''
        ];

        $playerTransfer=new PlayerTransfer();
        if (isset($request->status)) {
            $playerTransfer=$playerTransfer->where('status', (integer)$request->status);
            $params['status'] =(integer) $request->status;
        }
        if (isset($request->transferid)) {
            $playerTransfer=$playerTransfer->where(function ($query) use ($request) {
                $query->where('transferid', $request->transferid)->orWhere('plattransferid', $request->transferid);
            });

            $params['transferid'] = $request->transferid;
        }

        $playerTransfers = $playerTransfer->with('player','plat')->paginate((integer)$limit);
        return view('backend.game.site.player_transfer', compact('playerTransfers', 'params','limit'));
    }

    //修改状态
    public function updateStatus(PlayerTransfer $playerTransfer,Request $request)
    {
        $this->authorize('updateStatus', $playerTransfer);

        if(!isset($playerTransfer->player)){
            return $this->response->array([
                    'status_code' => 500,
                    'message' => '确认失败！玩家不存在或已被删除！'
                ]
            );
        }

        if(!isset($playerTransfer->player->site)){
            return $this->response->array([
                    'status_code' => 500,
                    'message' => '确认失败！网站不存在或已被删除！'
                ]
            );
        }

        if(!isset($playerTransfer->plat)){
            return $this->response->array([
                    'status_code' => 500,
                    'message' => '确认失败！平台不存在或已被删除！'
                ]
            );
        }
        $siteScore=SiteScore::where('transferid',$playerTransfer->plattransferid)
            ->orWhere('transferid',$playerTransfer->transferid)
            ->whereIn('op',[SiteScore::SITE_PLAT_TRANSFER_IN,SiteScore::SITE_PLAT_TRANSFER_OUT])
            ->first();
        $site=$playerTransfer->player->site;
        $mainGamePlat=$playerTransfer->plat;
        $siteMainGamePlat = $playerTransfer->player->site
            ->MainGamePlat()
            ->where('conf_site_main_game_plats.main_game_plats_id', $mainGamePlat->id)
            ->first();

        if($request->status=='success'){
            if($playerTransfer->direction==PlayerTransfer::TRANSFER_TYPE_IN){
                $playerTransfer->status=1;
                $siteScore->status=1;
            }elseif($playerTransfer->direction==PlayerTransfer::TRANSFER_TYPE_OUT){
                $playerTransfer->status=1;
                $sitescore = new SiteScore();
                $sitescore->player_id = $playerTransfer->player_id;
                $sitescore->site_id = $site->id;
                $sitescore->main_game_plats_id = $playerTransfer->main_game_plats_id;
                $sitescore->rate = $siteMainGamePlat->rate;
                $sitescore->before_money = $site->money;
                $sitescore->now_money = $site->money;
                $sitescore->recharge_score = $playerTransfer->money;
                $sitescore->original = $siteMainGamePlat->score;
                $siteMainGamePlat->score = bcadd($siteMainGamePlat->score, $playerTransfer->money, 5);
                $siteMainGamePlat->update();
                $sitescore->change = $siteMainGamePlat->score;
                $sitescore->transferid = $playerTransfer->plattransferid??$playerTransfer->transferid;
                $sitescore->op = SiteScore::SITE_PLAT_TRANSFER_OUT;
                $sitescore->status=1;
                $sitescore->save();
            }
        }elseif ($request->status=='failed'){
            if($playerTransfer->direction==PlayerTransfer::TRANSFER_TYPE_IN){
                $score=$siteMainGamePlat->pivot->score+$playerTransfer->money;
                $site->MainGamePlat()->updateExistingPivot($mainGamePlat->id,['score'=>$score]);
                $playerTransfer->status=0;
                $siteScore->status=0;
            }elseif ($playerTransfer->direction==PlayerTransfer::TRANSFER_TYPE_OUT){
                $playerTransfer->status=0;
                $siteScore->status=0;
            }
        }
        $playerTransfer->save();

        return $this->response->array([
                'status_code' => 200,
                'message' => '确认成功！'
            ]
        );

    }
}
